// miniprogram/pages/detail/detail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datail:{},
    isLike:false,
    ruleData:{},
    ruleType:[],
    count:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var id =  options.id
    console.log('id==>',id)
    this.getProductDetail(id)

  },
  getProductDetail:function(id){

    wx.showLoading({
      title: '加载中...',
    })

    wx.cloud.callFunction({
      name:'get_detail',
      data:{
        id:id,
      },  
      success:res=>{

        wx.hideLoading()

        var desc = res.result.data[0].desc

        res.result.data[0].desc = desc.split(/\n/)

       
        this.setData({
          datail:res.result.data["0"]
        })
      },
      fail:err=>{
        wx.hideloading();
        console.log('err==>',err)
      }
    })
  
    this.getLikeProduct(id)

    this.getRule(id)
  },
  //收藏或取消商品收藏
  likeProduct:function(e){


    if(!app.globalData.isAuth){

      wx.navigateTo({
        url: '../auth/auth',
      })


      return

    }



    wx.showLoading({
      title: '加载中...',
    })




    if(!this.data.isLike){

      wx.cloud.callFunction({
        name:'like_product',
        data:{
          id:e.currentTarget.dataset.id
        },
        success:res =>{

          console.log('res=>',res)

          wx.hideLoading({
            success: (res) => {},
          })

          this.setData({
            isLike:true
          })

        },
        fail:err=>{
          console.log('云函数调用失败 err=>',err)
          wx.hideLoading({
            success: (res) => {},
          })
        }
      })
      
    }else{

      wx.cloud.callFunction({
        name:'remove_like_product',
        data:{
          id:e.currentTarget.dataset.id
        },
        success:res=>{

          wx.hideLoading({
            success: (res) => {},
          })

          this.setData({
            isLike:false
          })

          
        },
        fail:error=>{

          wx.hideLoading({
            success: (res) => {},
          })
          console.log('error=>',error)

        }
      })

    }

      

  },

  //获取收藏商品
  getLikeProduct:function(id){

    wx.showLoading({
      title: '加载中...',
    })

    wx.cloud.callFunction({
      name:'get_like_product',
      data:{id:id},
      success:res=>{

        this.setData({
          isLike:res.result.data.length > 0
        })

        wx.hideLoading({
          success: (res) => {},
        })


      },
      fail:error=>{

        wx.hideLoading({
          success: (res) => {},
        })

      }
    })

  },
  //获取商品规格
  getRule:function(id){

    wx.showLoading({
      title: '加载中...',
    })

    wx.cloud.callFunction({
      name:'get_rule',
      data:{id:id},
      success:res=>{

        

        delete res.result.data[0]._id;

        delete res.result.data[0].id;

        for(let key in res.result.data[0]){

          if(res.result.data[0][key].value.length == 0){

            delete res.result.data[0][key];
          }

        }

        this.setData({
          ruleData:res.result.data[0],
          ruleType:Object.keys(res.result.data[0])
        })

        wx.hideLoading({
          success: (res) => {},
        })


      },
      fail:error=>{

        wx.hideLoading({
          success: (res) => {},
        })

      }
    })

  },

  //修改商品规格
  toggleRule:function(e){

    
    if(e.currentTarget.dataset.isselect){

      return;
    }

    var dataType = this.data.ruleData[e.currentTarget.dataset.key].value

    

    for(var i = 0; i < dataType.length ; i++ ){


      if(dataType[i].isSelect){

        this.data.ruleData[e.currentTarget.dataset.key].value[i].isSelect = false

        break;
      }

    }



    this.data.ruleData[e.currentTarget.dataset.key].value[e.currentTarget.dataset.index].isSelect = true


    this.setData({
      ruleData:this.data.ruleData
    })


  },

  //增加商品数量
  increase:function(){

    this.setData({
      count:this.data.count+1
    })

  },
  //减少商品数量
  decrease:function(){

    if(this.data.count == 1){

      return;
    }

    this.setData({
      count:this.data.count-1
    })

  },

  //加入购物车
  addShopcart:function(e){

    if(!app.globalData.isAuth){

      wx.navigateTo({
        url: '../auth/auth',
      })


      return

    }

    var rule = this.data.ruleData;

    var ruleVal = []

    for(var key in rule){

      var ruleItem = this.data.ruleData[key].value

      for(var i = 0 ; i < ruleItem.length ; i++){

        if(ruleItem[i].isSelect){

          ruleVal.push(ruleItem[i].v)
        }
      }

    }


    var product = {

      id:this.data.datail._id,
      count:this.data.count,
      rule:ruleVal.join('/'),
      isSelect:false
    }

    wx.showLoading({
      title: '加载中....',
    })

    wx.cloud.callFunction({

      name:'get_shopcart',
      data:{
        id:product.id,
        rule:product.rule,
      },
      success:res=>{

        wx.hideLoading()

        var k = e.currentTarget.dataset.key

        if(res.result.data.length==0 || k == 1){

          
          this.addShopcartDb(product,k)

        }else{

          product.count = res.result.data[0].count + this.data.count

          this.updateShopcartDb(product)

        }


      },
      fail:error=>{
        console.log('云函数调用出错 error==>',error)
      }
    })

    

  }
  ,
  addShopcartDb:function(product,key){

    wx.showLoading({title:'加载中...'})

    wx.cloud.callFunction({
      name:'add_shopcart',
      data:product,
      success:res=>{

        wx.hideLoading()

        console.log('res==>',res)

        if(key == 1){

          wx.navigateTo({
            url: '../commit/commit?id=' + res.result._id,
          })

        }
      },
      fail:erro=>{
        console.log('云函数调用失败 fail=>',fail)
      }
    })

  }
  ,
  updateShopcartDb:function(product){

    wx.showLoading({
      title: '加载中...',
    })

    wx.cloud.callFunction({
      name:'updataShopcart',
      data:product,
      success:res=>{

        wx.hideLoading()

        // console.log("res==>",res)

      },
      fail:error=>{

        wx.hideLoading()
        console.log('云函数调用失败==>',error)
      }
    })



  }
  ,
  buy:function(e){

    this.addShopcart(e)

  }





})